/*用于字典类型和接口类型不匹配时转换*/

/**
 * "0" -> false
 * "1" -> true
 * 其他 -> undefined, 不会传递给后端
 *
 * @param str
 * @returns {boolean}
 */
function stringToBoolean(str) {
  if (str === "0") {
    return false;
  } else if (str === "1") {
    return true;
  }
  return undefined;
}

function booleanToString(bool) {
  return bool ? "1" : "0";
}

/**
 * @Author:金雅蓓
 * @LastEditors: 金雅蓓
 * @Description: 字典转换
 * @param {Array} arr 当前获取的数组
 * @param {Array} directorayList 字典数组
 * @param {string} key 要转换的值
 */
function getDirectorayConver(arr, directorayList, key) {
  const currentList = arr.reduce((prev, cur) => {
    const current = directorayList.find((item) => item.value == cur[key]);
    if (current) {
      cur[`${key}Name`] = current.label;
    }
    prev.push(cur);
    return prev;
  }, []);
  return currentList;
}

module.exports = {
  stringToBoolean,
  booleanToString,
  getDirectorayConver,
};
